package cn.org.eshow.dao.hibernate;

import cn.org.eshow.bean.query.RechargeQuery;
import cn.org.eshow.common.dao.EnhancedRule;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.RechargeDao;
import cn.org.eshow.model.Recharge;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository("rechargeDao")
public class RechargeDaoHibernate extends GenericDaoHibernate<Recharge, Integer> implements RechargeDao {


    public RechargeDaoHibernate() {
        super(Recharge.class);
    }

    public Page<Recharge> search(RechargeQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        if (query != null) {
            rule.setOffset(query.getOffset());
            rule.setPageSize(query.getPagesize());
        }
        return page(rule);
    }

    @Override
    public Long count(RechargeQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return count(Recharge.class, rule);
    }

    public List<Recharge> list(RechargeQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return list(rule);
    }


    public EnhancedRule setRule(EnhancedRule rule, RechargeQuery query) {
        if (query != null) {
            if (query.startAddTime != null) {
                rule.add(Restrictions.ge("addTime", query.startAddTime));
            }
            if (query.endAddTime != null) {
                rule.add(Restrictions.le("addTime", query.endAddTime));
            }
            if (query.startUpdateTime != null) {
                rule.add(Restrictions.ge("updateTime", query.startUpdateTime));
            }
            if (query.endUpdateTime != null) {
                rule.add(Restrictions.le("updateTime", query.endUpdateTime));
            }
            if (query.no != null) {
                rule.add(Restrictions.like("no", query.no.trim(), MatchMode.ANYWHERE));
            }
            if (query.price != null) {
                rule.add(Restrictions.ge("price", query.price));
            }
            if (query.state != null) {
                rule.add(Restrictions.eq("state", query.state));
            }
            if (!StringUtils.isEmpty(query.channel)) {
                rule.add(Restrictions.like("channel", query.channel, MatchMode.ANYWHERE));
            }
            if (!StringUtils.isEmpty(query.remark)) {
                rule.add(Restrictions.like("remark", query.remark, MatchMode.ANYWHERE));
            }
            if (query.callback != null) {
                rule.add(Restrictions.like("callback", query.callback, MatchMode.ANYWHERE));
            }
            if (query.userId != null) {
                rule.add(Restrictions.eq("user.id", query.userId));
            }
            if (!StringUtils.isEmpty(query.getOrder())) {
                rule.addOrder(query.getDesc() ? Order.desc(query.getOrder()) : Order.asc(query.getOrder()));
            }
        }
        return rule;
    }

}
